﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class BalanceCenter_AdminUserEdit : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            dtUserBirthday.Value = "1970-01-01";

            FillProvince();

            FillddlBank();

            long id = -1;
            id = Shove._Convert.StrToLong(this.Request["id"], -1);
            if (id < 0)
            {
                btnSave.Enabled = false;
                btnSave.Visible = false;

                btnNetEdit.Enabled = false;
                btnNetEdit.Visible = false;

                btnUserNet.Enabled = false;
                btnUserNet.Visible = false;
                btnUserScore.Enabled = false;
                btnUserScore.Visible = false;
            }
            else
            {
                tbID.Text = id.ToString();

                BindData(id);
            }            
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        //RequestCompetences = Competences.MemberManagement;
        //RequestLoginPage = "BalanceCenter/AdminUserEdit.aspx";

        base.OnInit(e);
    }

    #endregion

    private void FillProvince()
    {
        DataTable dt = new DAL.Tables.tbl_Provinces().Open("", "", "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlUserProvince, dt, "Name", "ID");        

        if (ddlUserProvince.Items.Count > 0)
        {
            ddlUserProvince.SelectedIndex = 0;

            FillCity(int.Parse(ddlUserProvince.SelectedValue));
        }
    }

    private void FillCity(int ProvinceID)
    {
        DataTable dt = new DAL.Tables.tbl_Citys().Open("", "ProvinceID = " + ProvinceID.ToString(), "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlUserCity, dt, "Name", "ID");

        if (ddlUserCity.Items.Count > 0)
        {
            ddlUserCity.SelectedIndex = 0;
        }
    }

    private void FillddlBank()
    {
        DataTable dt = new DAL.Tables.tbl_Banks().Open("", "", "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlUserBankName, dt, "Name", "ID");
    }

    protected void ddlUserProvince_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
    }

    private void BindData(long id)
    {
        Customer customer = new Customer(_Site.ID);
        customer.ID = id;
        string Return = "";
        int ResultValue = 0;

        int Result = customer.GetCustomerInformationByID(ref Return);
        if (Result != 0)
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);

            return;
        }

        if (ResultValue < 0)
        {
            PublicFunction.GoError(ErrorNumber.Unknow, Return, this.GetType().BaseType.FullName);

            return;
        }

        BindData(customer);
    }

    private void BindData(Customer customer)
    {
        DataTable dt = new DAL.Tables.tbl_Citys().Open("ProvinceID", "[id] = " + customer.CityID.ToString(), "[id]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        long ProvinceID = -1;
        try
        {
            ProvinceID = Shove._Convert.StrToLong(dt.Rows[0][0].ToString(), -1);
        }
        catch { }

        if (ProvinceID < 0)
        {
            ProvinceID = 1;
            customer.CityID = 1;
        }

        Shove.ControlExt.SetDownListBoxTextFromValue(ddlUserProvince, ProvinceID.ToString());
        FillCity(int.Parse(ddlUserProvince.SelectedValue));
        Shove.ControlExt.SetDownListBoxTextFromValue(ddlUserCity, customer.CityID.ToString());
        Shove.ControlExt.SetDownListBoxTextFromValue(ddlUserBankName, customer.BankTypeID.ToString());

        tbUserName.Text = customer.CustomerNO;
        tbUserRealityName.Text = customer.CustomerName;
        tbUserIDCardNumber.Text = customer.CertNo;

        string Return = "";

        if (customer.CommendID != -1)
        {
            Customer Commend = new Customer(_Site.ID);
            Commend.ID = customer.CommendID;
            int Result = Commend.GetCustomerInformationByID(ref Return);
            tbCommendNO.Text = Commend.CustomerNO;
        }

        tbUserEmail.Text = customer.Email;
        tbUserTelephone.Text = customer.Tel;
        tbUserMobile.Text = customer.Mobile;
        tbUserQQ.Text = customer.QQ;
        tbUserAddress.Text = customer.Address;
        if (customer.Sex == "男")
        {
            rbUserSexM.Checked = true;
        }
        else if (customer.Sex == "女")
        {
            rbUserSexW.Checked = true;
        }
        dtUserBirthday.Value = customer.Birthday.ToString("yyyy-MM-dd");

        Shove.ControlExt.SetDownListBoxTextFromValue(ddlUserBankName, customer.BankTypeName);
        tbUserBankNameDetail.Text = customer.BankNameDetail;
        tbUserBankCardNumber.Text = customer.BankCardNumber;
        tbAlipayNo.Text = customer.AlipayNo;
        cbSurrogate.Checked = customer.IsSurrogate;

        labUserType.Text = "";

        tbID.Text = customer.ID.ToString();

        btnSave.Enabled = true;
        btnSave.Visible = true;

        btnNetEdit.Enabled = false;
        btnNetEdit.Visible = false;

        btnUserNet.Enabled = true;
        btnUserNet.Visible = true;
        btnUserScore.Enabled = true;
        btnUserScore.Visible = true;

        //会员类别: 结算会员｜商城会员
        if (customer.CustomerTypeID == 11)
        {
            btnUserScore.Enabled = false;
            btnUserScore.Visible = false;
            btnUserNet.Enabled = false;
            btnUserNet.Visible = false;
        }

        //BindDataForScore(customer);
    }

    private void BindDataForScore(Customer customer)
    {

        //DataSet ds = DAL.Procedures.P_GetScoreForUser.Open(tu.ID, ref L, ref C, ref R, ref L_CurrentMonth, ref C_CurrentMonth, ref R_CurrentMonth, ref L_CurrentWeek, ref C_CurrentWeek, ref R_CurrentWeek, ref L_CurrentDay, ref C_CurrentDay, ref R_CurrentDay);

        //if (ds == null)
        //{
        //    Shove._Web.JavaScript.Alert(this, "数据库连接失败");

        //    return;
        //}


        //Customer customer_2 = new Customer();
        //string Return = "";

        //if (tu.CommendID > 0)
        //{
        //    tu_2.ID = tu.CommendID;
        //    if (tu_2.GetUserInfoByID(ref Return) == 0)
        //    {
        //        labCommend.Text = tu_2.Name + "," + tu_2.RealityName;
        //    }
        //}

        //int ResutValue = 0;
        //if (tu.ParentID > 0)
        //{
        //    tu_2.ID = tu.ParentID;
        //    if (tu_2.GetUserInfoByID(ref Return) == 0)
        //    {
        //        labParent.Text = tu_2.Name + "," + tu_2.RealityName;
        //    }
        //}
    }

    protected void btnRead_Click(object sender, System.EventArgs e)
    {
        if (tbUserName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入用户名。");

            return;
        }

        string Return = "";

        Customer Customer = new Customer(_Site.ID);
        Customer.CustomerNO = tbUserName.Text.Trim();

        int Result = Customer.GetCustomerInformationByNO( ref Return);
        if (Result != 0)
        {
            btnSave.Enabled = false;
            btnSave.Visible = false;
            btnNetEdit.Enabled = false;
            btnNetEdit.Visible = false;
            btnUserNet.Enabled = false;
            btnUserNet.Visible = false;

            Shove._Web.JavaScript.Alert(this.Page, Return);
            return;
        }

        labSLM.Text = "";
        labSCM.Text = "";
        labSRM.Text = "";

        labSLW.Text = "";
        labSCW.Text = "";
        labSRW.Text = "";

        labSL.Text = "";
        labSC.Text = "";
        labSR.Text = "";
        labCommend.Text = "";
        labParent.Text = "";
        labLCR.Text = "";

        BindData(Customer);
    }

    protected void btnSave_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        Customer customer = new Customer(_Site.ID);
        customer.ID = int.Parse(tbID.Text);

        string Return = "";
        int ResultValue = 0;

        int Result = customer.GetCustomerInformationByID(ref Return);
        if ((Result != 0) || (ResultValue < 0))
        {
            Shove._Web.JavaScript.Alert(this.Page, Return);

            return;
        }
        if (customer.CustomerNO != tbUserName.Text.Trim())
        {
            Shove._Web.JavaScript.Alert(this.Page, "不能修改用户名。");

            return;
        }

        if (tbUserRealityName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入真实姓名。");

            return;
        }

        if (cbUpdatePassword.Checked)
        {
            if (tbUserPassword.Text == "")
            {
                Shove._Web.JavaScript.Alert(this.Page, "请输入用户密码。");

                return;
            }
        }

        if (cbUpdatePassword2.Checked)
        {
            if (tbUserPassword2.Text == "")
            {
                Shove._Web.JavaScript.Alert(this.Page, "请输入用户二级密码。");

                return;
            }
        }

        if (tbUserMobile.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入手机号码。");

            return;
        }

        if (tbUserBankNameDetail.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入详细开户银行名称。");

            return;
        }

        if (tbUserBankCardNumber.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入银行卡号。");

            return;
        }

        //if (tbPayNO.Text.Trim() == "")
        //{
        //    Shove._Web.JavaScript.Alert(this.Page, "请输入支付宝号。");

        //    return;
        //}

        customer.CustomerName = Shove._Convert.ToDBC(tbUserRealityName.Text.Trim());
        customer.Sex = rbUserSexM.Checked ? "男" : (rbUserSexW.Checked ? "女" : "");
        customer.Birthday = Shove._Convert.StrToDateTime(dtUserBirthday.Value, DateTime.Now.ToShortDateString());

        if (cbUpdatePassword.Checked)
        {
            customer.Password1 = tbUserPassword.Text.Trim();//PublicFunction.EncryptPassword(tbUserPassword.Text.Trim());
        }

        if (cbUpdatePassword2.Checked)
        {
            customer.Password2 = tbUserPassword2.Text.Trim();//PublicFunction.EncryptPassword(tbUserPassword2.Text.Trim());
        }

        customer.Email = Shove._Convert.ToDBC(tbUserEmail.Text.Trim());
        customer.CityID = int.Parse(ddlUserCity.SelectedValue);

        customer.CertNo = Shove._Convert.ToDBC(tbUserIDCardNumber.Text.Trim());
        customer.Tel = Shove._Convert.ToDBC(tbUserTelephone.Text.Trim());
        customer.Mobile = Shove._Convert.ToDBC(tbUserMobile.Text.Trim());
        customer.QQ = Shove._Convert.ToDBC(tbUserQQ.Text.Trim());
        customer.Address = Shove._Convert.ToDBC(tbUserAddress.Text.Trim());
        customer.BankTypeID = Shove._Convert.StrToShort(ddlUserBankName.SelectedValue, 1);
        customer.BankNameDetail = tbUserBankNameDetail.Text.Trim();
        customer.BankCardNumber = Shove._Convert.ToDBC(tbUserBankCardNumber.Text.Trim());
        customer.AlipayNo = tbAlipayNo.Text;

        customer.IsSurrogate = cbSurrogate.Checked;

        Return = "";
        ResultValue = 0;

        if (customer.EditCustomerByID(ref Return) != 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, Return);

            return;
        }

        if (customer.ID == _Customer.ID)
        {
            _Customer.GetCustomerInformationByID(ref Return);
        }

        Shove._Web.JavaScript.Alert(this.Page, "用户资料已经保存成功。");
    }

    protected void btnNetEdit_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        this.Response.Redirect("AdminUserNetEdit.aspx?id=" + tbID.Text);
    }

    protected void btnUserNet_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        this.Response.Redirect("Net.aspx?id=" + tbID.Text);
    }

    protected void btnUserScore_Click(object sender, System.EventArgs e)
    {
        if (tbID.Text == "")
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);
            return;
        }

        this.Response.Redirect("ScoreEarning.aspx?id=" + tbID.Text);
    }
}
